﻿using System;
using Engine;
using Engine.Graphics;
using Engine.Input;
using Engine.Media;
using Game;

namespace Mekiasm
{
    public class EditTextWidget : CanvasWidget
    {

        public BevelledRectangleWidget bevelledRectangle = new BevelledRectangleWidget() { BevelColor = Color.White, BevelSize = 1f, CenterColor = Color.Black };
        public LabelWidget labelWidget = new LabelWidget() { FontScale = 0.7f, Color = Color.Gray, IsVisible = true };
        public LabelWidget show = new LabelWidget() { FontScale = 0.7f, Color = Color.Gray, IsVisible = true };
        public bool CursorShow = false;
        public float FontScale
        {
            get { return labelWidget.FontScale; }
            set { labelWidget.FontScale = value; show.FontScale = value; }
        }
        public Vector2 Size_
        {
            get { return Size; }
            set { Size = value; bevelledRectangle.Size = value; show.Size = value; labelWidget.Size = value; }

        }
        public event Action<string> TextChange;
        public bool HasFocus { get; set; }
        public float CursorX = 0f;
        public string PlaceHolder
        {
            get { return labelWidget.Text; }
            set { labelWidget.Text = value; }
        }
        public string Text
        {
            get { return show.Text; }
            set { show.Text = value; }
        }
        public EditTextWidget()
        {
            TextChange += (box) =>
            {
                if (string.IsNullOrEmpty(Text)) labelWidget.IsVisible = true;
                else labelWidget.IsVisible = false;
                CursorX = 0;
                foreach (char ch in Text)
                {
                    BitmapFont.Glyph bitmapFont = labelWidget.Font.GetGlyph(ch);
                    CursorX += bitmapFont.Width;
                    CursorX += labelWidget.Font.Spacing.X;
                }
            };
            Children.Add(bevelledRectangle);
            Children.Add(labelWidget);
            Children.Add(show);
            IsDrawRequired = true;
            Keyboard.CharacterEntered += (c) =>
            {

                if (HasFocus)
                {
                    Text += c;
                    TextChange?.Invoke(Text);
                }
            };
        }

        public override void Update()
        {
            if (Input.Click.HasValue)
            {
                if (HitTestGlobal(Input.Click.Value.Start) == this && HitTestGlobal(Input.Click.Value.End) == this)
                {
                    HasFocus = true;

                }
                else
                {
                    HasFocus = false;
                }
            }
            if (HasFocus && Text.Length > 0)
            {
                if (Keyboard.IsKeyDownRepeat(Key.BackSpace) || Keyboard.IsKeyDownRepeat(Key.Delete))
                {
                    Text = Text.Remove(Text.Length - 1, 1);
                    TextChange?.Invoke(Text);
                }

            }
        }
        public override void Draw(DrawContext dc)
        {
            base.Draw(dc);
            if (Time.PeriodicEvent(0.5, 0.0)) CursorShow = !CursorShow;
            if (CursorShow && HasFocus)
            {
                FlatBatch2D flatBatch2D = dc.PrimitivesRenderer2D.FlatBatch();
                Vector2 top = new Vector2(CursorX, 0);
                Vector2 bottom = new Vector2(CursorX + 1, labelWidget.Font.GlyphHeight);
                int count = flatBatch2D.TriangleVertices.Count;
                flatBatch2D.QueueQuad(top, bottom, 1f, Color.Green);
                flatBatch2D.TransformTriangles(GlobalTransform, count);

            }
        }
    }
}
